//
//  AppDelegate.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourViewController.h"
#import "LeftChouTiViewController.h"
#import "UMSocial.h"
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@end

@implementation AppDelegate

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"模式" object:nil];
    [_mmDrawer release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //启动页延迟
//    [NSThread sleepForTimeInterval:2.0];
    
    //夜间模式
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(decideMode:) name:@"模式" object:nil];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"mode"];
    if (!str) {
        [[NSUserDefaults standardUserDefaults] setObject:@"day" forKey:@"mode"];
    }

    
    //分享
    [UMSocialData setAppKey:@"56a87643e0f55a9fc20020c2"];
    
    
#pragma mark - left我的页面 左视图
    //left
    LeftChouTiViewController *left = [[LeftChouTiViewController alloc]init];
    UINavigationController *leftuina = [[UINavigationController alloc]initWithRootViewController:left];
   
    left.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"pinkFlower"] tag:1004];
#pragma mark - 首页
    //创建首页ViewControl
    FirstViewController *first = [[FirstViewController alloc]init];
    //创建首页Navigation
    self.navi = [[UINavigationController alloc]initWithRootViewController:first];
    //创建首页tabbar按钮
    first.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"work"] tag:1000];
    
#pragma mark - 购买
    //创建购买页面ViewControl
    SecondViewController *sec= [[SecondViewController alloc]init];
    //创建购买页面的navigation
    self.secondnavi = [[UINavigationController alloc]initWithRootViewController:sec];
    //创建购买页面tabbar
    sec.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"购买" image:[UIImage imageNamed:@"shop"] tag:1001];
    
#pragma mark - 礼物
    //创建礼物页面ViewControl
    ThirdViewController *third = [[ThirdViewController alloc]init];
    //创建礼物页面的navigation
   self.thirduina= [[UINavigationController alloc]initWithRootViewController:third];
    //创建礼物页面tabbar
    third.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"礼物" image:[UIImage imageNamed:@"gift"] tag:1002];
#pragma mark - 其他
    //创建其他页面ViewControl
    FourViewController *four = [[FourViewController alloc]init];
    //创建其他页面的navigation
   self.fouruina = [[UINavigationController alloc]initWithRootViewController:four];
    //创建其他页面tabbar
    four.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"教程" image:[UIImage imageNamed:@"user"] tag:1003];
    
#pragma mark - //tabbar
    UITabBarController *tabbar = [[UITabBarController alloc]init];
    tabbar.viewControllers = @[_navi, _secondnavi, _thirduina, _fouruina,leftuina];
    //tabbar 按钮颜色
    tabbar.tabBar.tintColor = [UIColor colorWithRed:236/255.0 green:135/255.0 blue:14/255.0 alpha:1];

    self.window.rootViewController = tabbar;
    
    [first release];//首页
    [sec release];//购买
    [third release];//礼物
    [four release];//其他
    [_navi release];//首页导航栏
    [_secondnavi release];////购买页面导航栏
    [_thirduina release];//礼物页面导航栏
    [_fouruina release];//其他页面导航栏
    [tabbar release];//tabbar
    [left release];//左视图
    [self.window release];
    
    [self guideScroll];
    return YES;
    
    
    
}

- (void)decideMode:(NSNotification *)noti;
{
    if ([noti.object isEqualToString:@"Night"]) {
        self.window.alpha = 0.7;
    }else{
        self.window.alpha = 1;
    }
}

- (void)guideScroll
{
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"firstLaunch"]== NO)
    {
        NSArray *imagesArray = @[@"yinfao.jpg", @"yindao2.jpg", @"123.jpg"];
        //判断程序是否第一次启动
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        //第一次启动操作   scrollView
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.window.frame];
        self.scrollView.delegate = self;
        [self.scrollView setContentSize:CGSizeMake(self.window.frame.size.width * imagesArray.count, 0)];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        [self.scrollView setPagingEnabled:YES];
        [self.scrollView showsHorizontalScrollIndicator];
        [self.scrollView setBounces:NO];
        [_scrollView release];
        for (int i = 0; i < imagesArray.count; i++) {
            
            UIImageView *images =[[UIImageView alloc]initWithFrame:CGRectMake(i * self.window.frame.size.width, 0, self.window.frame.size.width, [UIScreen mainScreen].bounds.size.width)];
            [images setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
            [self.scrollView addSubview:images];
            [images release];
        }
    //进入的Botton
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake((imagesArray.count - 1) * self.window.frame.size.width + self.window.frame.size.width / 2 + 10, self.window.frame.size.height * 0.88, 120, 40)];
        [button setTitle:@"进入" forState:UIControlStateNormal];//按钮
        [button.titleLabel setFont:[UIFont systemFontOfSize:22]];
        button.layer.cornerRadius = 5;//圆角
        button.layer.borderWidth = 2;//边框粗细
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
        //button颜色
        [button setTitleColor: [UIColor colorWithRed:0.78 green:0.34 blue:0.31 alpha:1]forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
        [self.window addSubview:self.scrollView];
    }
}



- (void)buttonClick {
    [UIView animateWithDuration:1 animations:^{
        self.scrollView.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self.scrollView removeFromSuperview];
    }];
}


//三方登录回调方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
    }
    NSLog(@"登录成功");
    return result;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
